import { useHttp } from './useHttp'

export function getRequestIUrl(service: string, type = 'select', app: any) {
  const config = useRuntimeConfig()
  const baseApp = app || config.public.baseApp
  const baseApi = config.public.baseApi
  return `${baseApi}/${baseApp}/${type}/${service}`
}

export function onLogin(data) {
  const req = [{
    serviceName: 'srvuser_login',
    data: [{ user_no: data.user_no, pwd: data.pwd, terminal_type: 'PC' }],
  }]
  const url = getRequestIUrl('srvuser_login', 'operate', 'sso')
  return useHttp.post(url, req)
}



export function userRegister(reqBody = {}){
  const config = useRuntimeConfig()
  const baseApp = config.public.baseApp
  const baseApi = config.public.baseApi
  const service =  `${baseApi}/${baseApp}/operate/srvjk_member_add`
  const req = [
    {
      "serviceName": "srvjk_member_add",
      "condition": [],
      "data": [
        {
          "nickname": reqBody.nickname,
          "tel": reqBody.phone,
          "email": reqBody.email,
          "status": 1
        }
      ]
    }
  ]
  return useHttp.post(service, req)
}

export async function getScanConfig(callbackurl) {
  const request = {}
  request.serviceName = 'srvwx_web_scan_cfg_select'
  request.colNames = ['*']
  request.condition = [{ colName: 'callbackurl', value: callbackurl }]
  const url = getRequestIUrl('srvwx_web_scan_cfg_select', 'select', 'wx')
  const rsp = await useHttp.post(url, request)
  const data = rsp.data[0]
  // if(data.bx_auth_ticket){
  //   sessionStorage.setItem("bx_auth_ticket", data.bx_auth_ticket)
  // }
  // var obj = new WxLogin({
  //   self_redirect: false,
  //   id: "login_container",
  //   appid: data.appid,
  //   scope: "snsapi_login",
  //   redirect_uri: data.callbackurl,
  //   state: data.state,
  //   style: "black",
  //   href: window.location.origin + "/main/css/loginqrcode.css"
  // });
}
// // 用户扫码授权处理
export function scanLogin(code, state) {
  const data = {}
  data.code = code
  data.state = state
  const request = {}
  request.serviceName = 'srvwx_web_scan_auth'
  request.data = []
  request.data.push(data)
  const bxRequest = []
  bxRequest.push(request)
  const url = getRequestIUrl('srvwx_web_scan_auth', 'operate', 'wx')
  return useHttp.post(url, bxRequest)
}
